<?php

namespace App\Http\Controllers\Customer\Api;

use App\Exceptions\DeviceException;
use App\Facades\ChargeRecordService;
use App\Facades\ConfigService;
use App\Facades\DeviceService;
use App\Facades\UserService;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cache;
use DB;
use Illuminate\Http\Request;
use Redis;

class DeviceController extends Controller
{
	public function bind(Request $request)
	{
		$rules = [
				'device_code' => 'required|exists:devices,code',
			];
		$this->validate($request, $rules);
		
        $config = json_decode(Redis::get('bindMessage'), true);
        //模板未定，测试数据
        if(is_null($config)){
            $config = config('heting.template');
        }
        $bindMessage = [
            'first' => $config['content1'],
            'keyword1' => strtolower($request->device_code),
            'keyword2' => $config['content2'],
            'remark' => $config['content3'],
        ];
		DeviceService::bind(strtolower($request->device_code), $bindMessage);
		return formatJsonResponse();
	}

    public function control(Request $request)
    {
        $rules = [
            'command' => 'required',
        ];
        $this->validate($request, $rules);
        
        if(!$request->is('admin/*') && ($request->command=='halt' && $request->command=='stop_halt')){
            throw new DeviceException('command');
        }
        DeviceService::control($request->command, $request->num);
        
        return formatJsonResponse();
    }
    
    public function getState(Request $request){
        $state = DeviceService::getState();
        return formatJsonResponse([
            'dec_status' => $state['decStatus'],
            'dec_status_description' => $state['decStatusDescription'],
            'effluent_status' => $state['effluentStatus'],
            'effluent_status_description' => $state['effluentStatusDescription'],
            'TDS1' => $state['TDS1'],
            'TDS2' => $state['TDS2'],
            'filter1' => $state['filter1'],
            'filter_cnt1' => $state['filterCnt1'],
            'filter2' => $state['filter2'],
            'filter_cnt2' => $state['filterCnt2'],
            'filter3' => $state['filter3'],
            'filter_cnt3' => $state['filterCnt3'],
            'filter4' => $state['filter4'],
            'filter_cnt4' => $state['filterCnt4'],
            'filter5' => $state['filter5'],
            'filter_cnt5' => $state['filterCnt5'],
            ]);
    }

    public function unbind(Request $request)
    {
        $rules = [
            'device_code' => 'required|exists:devices,code',
            'reset_user_info' => 'required|boolean'
        ];
        $this->validate($request, $rules);

        DeviceService::unbindByDeviceCode();
        if($request->reset_user_info){
            UserService::updateUserInfo($request->only([
                'name',
                'sex',
                'phone',
                'installation_address'
            ]));
        }
        return formatJsonResponse();
    }

    public function pay(Request $request)
    {
        $price = ChargeRecordService::getPrice();
        $rules = [
            'money' => 'required|integer',
            'flow' => 'required|integer|size:'.round($request->money/$price),
        ];
        $this->validate($request, $rules);

        $deviceCode = deviceCode();
        DeviceService::isOffline($deviceCode);
        DeviceService::canRecharge($deviceCode, 2);
        DeviceService::isExceedMaxRechargeAmount($deviceCode, $request->flow);
        return DB::transaction(function() use ($request) {
            $result = ChargeRecordService::wechatUnifiedPay($request->flow, $request->money);
            return formatJsonResponse($result);
        });
    }

    public function wechatPayNotify(){
        $body = file_get_contents('php://input');
        $result = ChargeRecordService::recharge($body); 
        return $result;
    }

}
